package lion.dev.io;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

import lion.dev.lang.MapJ;

public class MimeTypeUtil {

	private static MapJ mimeTypes = null;

	public static String getMimeType(String type) {

		String ext = FilenameUtils.getExtension(type.toLowerCase());
		if (mimeTypes == null) {
			init();
		}
		String mimetype = mimeTypes.getString(ext);
		if (StringUtils.isBlank(mimetype)) {
			mimetype = "application/octet-stream";
		}
		return mimetype;
	}

	public static void init() {

		mimeTypes = new MapJ();
		Properties properties = new Properties();
		try {
			properties.load(MimeTypeUtil.class.getResourceAsStream("/META-INF/mime.types"));
		} catch (IOException e) {
		}
		for (String key : properties.stringPropertyNames()) {
			mimeTypes.put(key, properties.getProperty(key));
		}
	}
}
